package com.matrix.review.controller;

import com.matrix.api.review.dto.ReviewDTO;
import com.matrix.api.review.dto.ReviewReplyDTO;
import com.matrix.api.review.vo.ReviewVO;
import com.matrix.common.domain.PageResult;
import com.matrix.common.domain.Result;
import com.matrix.review.service.ReviewService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author 有点甜
 * @since 2025/7/30
 */
@RestController
@RequiredArgsConstructor
public class ReviewController {

    private final ReviewService reviewService;

    @PostMapping("/app/review")
    @PreAuthorize("hasAuthority('USER')")
    public Result<Void> submitReview(@RequestHeader("X-User-ID") Long userId,
                                     @Valid @RequestBody ReviewDTO reviewDTO) {
        reviewService.submitReview(userId, reviewDTO);
        return Result.success();
    }

    @GetMapping("/app/reviews/shop/{shopId}")
    public Result<PageResult<ReviewVO>> getReviewsForShop(
            @PathVariable("shopId") Long shopId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String filter) {
        return Result.success(reviewService.getReviews(shopId, "SHOP", pageNum, pageSize, filter));
    }

    @PostMapping("/merchant/review/reply/{reviewId}")
    @PreAuthorize("hasAuthority('USER')")
    public Result<Void> replyToReview(@RequestHeader("X-User-ID") Long userId,
                                      @PathVariable("reviewId") Long reviewId,
                                      @Valid @RequestBody ReviewReplyDTO replyDTO) {
        reviewService.replyToReview(userId, reviewId, replyDTO);
        return Result.success();
    }

    @GetMapping("/merchant/reviews")
    public Result<PageResult<ReviewVO>> getReviewsForMerchant(
            @RequestHeader("X-User-ID") Long userId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String filter) {

        return Result.success(reviewService.getReviewsForMerchant(userId, pageNum, pageSize, filter));
    }
}
